package com.sgb.kjwl.view.ui.adapter.transport;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarFuncationPopwindowsBinding;
import com.sgb.kjwl.model.entity.AllFunction;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CarFuncationPopWindow extends BasePopWindow implements View.OnClickListener {

    private ActivityCarFuncationPopwindowsBinding mBinding;
    //
    private CarFuncationAdapter mAdapter;
    private List<AllFunction> mList;

    public CarFuncationPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.activity_car_funcation_popwindows;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (ActivityCarFuncationPopwindowsBinding) holder.getmBinding();
        initRecycler();
        initEvent();
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new CarFuncationAdapter(mList);
        mBinding.rvList.setLayoutManager(new GridLayoutManager(mContext, 4));
        DividerDecoration.builder(mContext)
                .color(Color.TRANSPARENT)
                .size(6, TypedValue.COMPLEX_UNIT_DIP)
                .build()
                .addTo(mBinding.rvList);
        mAdapter.bindToRecyclerView(mBinding.rvList);
    }

    private void initEvent() {
        mBinding.setOnClick(this);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mAdapter.notifyDataSetChanged();
        });
    }

    public void setData(List<AllFunction> list) {
        mList.clear();
        if (list != null) {
            mList.addAll(list);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.tvCancel) {
            dissMiss();
        } else if (v == mBinding.tvSure) {
            List<AllFunction> entity = mAdapter.getData();
            if (entity == null) {
                MToast.showToast(mContext, "请选择车辆功能");
                return;
            }
            onResultClick.result(mAdapter.getData(), null, null);
            dissMiss();
        }
    }
    private static class CarFuncationAdapter extends BaseQuickAdapter<AllFunction, BaseViewHolder> {
        HashMap<String, AllFunction> mSelects = new HashMap<>();
        public CarFuncationAdapter(@Nullable List<AllFunction> data) {
            super(R.layout.car_funcation_pop_item, data);

        }
        @Override
        protected void convert(BaseViewHolder helper, AllFunction item) {
            if (item.isIsselect()){
                mSelects.put(item.getNo(),item);
                helper.getView(R.id.tv_name_car).setSelected(item.isIsselect());
            }else{
                helper.getView(R.id.tv_name_car).setSelected(false);
            }
            helper.setText(R.id.tv_name_car,item.getName());


            helper.getView(R.id.tv_name_car).setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onClick(View v) {
                    if (mSelects!=null&&mSelects.size()==5){
                        if(helper.getView(R.id.tv_name_car).isSelected()){
                            helper.getView(R.id.tv_name_car).setSelected(false);
                            item.setIsselect(false);
                            mSelects.remove(item.getNo(),item);
                            return;
                        }
                        MToast.showToast(helper.itemView.getContext(),"最多任选5项");
                        return;
                    }

                    if (!item.isIsselect()){
                        helper.getView(R.id.tv_name_car).setSelected(true);
                        item.setIsselect(true);
                        mSelects.put(item.getNo(),item);
                    }else {
                        helper.getView(R.id.tv_name_car).setSelected(false);
                        item.setIsselect(false);
                        mSelects.remove(item.getNo(),item);
                    }
                }
            });
        }
    }
}
